package com.xxjs.pro.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.util
 * @Author: Master_XXJS
 * @CreateTime: 2022-09-17  22:30
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class MailClient {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * @description: 发送邮件
     * @author: Master_XXJS
     * @date: 2022/9/17 22:37
     * @param: [to 发送邮件的对象, subject 邮件主题, content 邮件正文]
     * @return: void
     **/
    public void sendMail(String to,String subject,String content){
        try {
            //创建封装邮件相关信息的对象  mimeMessage
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            //进行邮件的属性（基本信息）的指定
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            //发送邮件
            javaMailSender.send(helper.getMimeMessage());
        } catch (Exception e) {
            log.error("发送邮件失败："+e.getMessage());
        }
    }
}

